de-duplicate format setup in find_vec (#997)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Wed, 8 Feb 2023 15:34:41 +0000 (08:34 -0700)
committerGitHub <noreply@github.com>
Wed, 8 Feb 2023 15:34:41 +0000 (08:34 -0700)
* de-duplicate format setup in find_vec

* create a class to pass format info pack and forth from vecs.

This simplifies re-initialization of a format, which main already
did in a few cases.

The operator overloads for the format info class make the transition
simplier.

* return empty braced init list from find_vecs when not found.

format.h
garmin.cc
main.cc
vecs.cc
vecs.h

index 3774c59e84078e6d2ecc3260ff796906848490a4..44ef808134140be6bc5adf8153fdaa0dec0d3516 100644 (file)
--- a/format.h
+++ b/format.h
@@ -169,29 +169,6 @@ public:
   virtual ff_type get_type() const = 0;
   virtual QVector<ff_cap> get_cap() const = 0;
 
-  QString get_name() const
-  {
-    return name;
-  }
-
-  void set_name(const QString& nm)
-  {
-    name = nm;
-  }
-
-  QString get_argstring() const
-  {
-    return argstring;
-  }
-
-  void set_argstring(const QString& string)
-  {
-    argstring = string;
-  }
-private:
-  QString name;
-  QString argstring;
-
 protected:
   template <class MyFormat>
   class RteHdFunctor
index 63516c3ad4cd4002184b56ac61f41bd06d8dedab..59db848ef74c4323a3110166577d4300ef0cdff3 100644 (file)
--- a/garmin.cc
+++ b/garmin.cc
@@ -355,7 +355,7 @@ rd_init(const QString& fname)
 {
   if (setjmp(gdx_jmp_buf)) {
     const gdx_info* gi = gdx_get_info();
-    gpx_vec = Vecs::Instance().find_vec("gpx");
+    gpx_vec = Vecs::Instance().find_vec("gpx").fmt;
     gpx_vec->rd_init(gi->from_device.canon);
   } else {
     gpx_vec = nullptr;
diff --git a/main.cc b/main.cc
index d70c4ec4f7d22d786efc0cd9ea888e801426574d..809e057f67e259ce5ef6d4229a7f8490e7eb0cae 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -248,8 +248,8 @@ static int
 run(const char* prog_name)
 {
   int argn;
-  Format* ivecs = nullptr;
-  Format* ovecs = nullptr;
+  Vecs::fmtinfo_t ivecs;
+  Vecs::fmtinfo_t ovecs;
   Filter* filter = nullptr;
   QString fname;
   QString ofname;
@@ -310,17 +310,17 @@ run(const char* prog_name)
     case 'i':
       argument = FETCH_OPTARG;
       ivecs = Vecs::Instance().find_vec(argument);
-      if (ivecs == nullptr) {
+      if (!ivecs) {
         fatal("Input type '%s' not recognized\n", qPrintable(argument));
       }
       break;
     case 'o':
-      if (ivecs == nullptr) {
+      if (!ivecs) {
         warning("-o appeared before -i.   This is probably not what you want to do.\n");
       }
       argument = FETCH_OPTARG;
       ovecs = Vecs::Instance().find_vec(argument);
-      if (ovecs == nullptr) {
+      if (!ovecs) {
         fatal("Output type '%s' not recognized\n", qPrintable(argument));
       }
       break;
@@ -330,7 +330,7 @@ run(const char* prog_name)
       if (fname.isEmpty()) {
         fatal("No file or device name specified.\n");
       }
-      if (ivecs == nullptr) {
+      if (!ivecs) {
         fatal("No valid input type specified\n");
       }
       if (global_opts.masked_objective & POSNDATAMASK) {
@@ -342,7 +342,7 @@ run(const char* prog_name)
         global_opts.masked_objective |= WPTDATAMASK;
       }
 
-      start_session(ivecs->get_name(), fname);
+      start_session(ivecs.fmtname, fname);
       ivecs->rd_init(fname);
       ivecs->read();
       ivecs->rd_deinit();
@@ -533,16 +533,16 @@ run(const char* prog_name)
     }
 
     /* reinitialize xcsv in case two formats that use xcsv were given */
-    (void) Vecs::Instance().find_vec(ivecs->get_argstring());
+    Vecs::Instance().prepare_format(ivecs);
 
-    start_session(ivecs->get_name(), qargs.at(0));
+    start_session(ivecs.fmtname, qargs.at(0));
     ivecs->rd_init(qargs.at(0));
     ivecs->read();
     ivecs->rd_deinit();
 
     if (qargs.size() == 2 && ovecs) {
       /* reinitialize xcsv in case two formats that use xcsv were given */
-      (void) Vecs::Instance().find_vec(ovecs->get_argstring());
+      Vecs::Instance().prepare_format(ovecs);
 
       ovecs->wr_init(qargs.at(1));
       ovecs->write();
@@ -553,7 +553,7 @@ run(const char* prog_name)
     usage(prog_name,0);
     return 0;
   }
-  if (ovecs == nullptr) {
+  if (!ovecs) {
     auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void {
       fbOutput.waypt_disp(wpt);
     };
@@ -576,7 +576,7 @@ run(const char* prog_name)
     if (fname.isEmpty()) {
       fatal("An input file (-f) must be specified.\n");
     }
-    start_session(ivecs->get_name(), fname);
+    start_session(ivecs.fmtname, fname);
     ivecs->rd_position_init(fname);
 
     if (global_opts.masked_objective & ~POSNDATAMASK) {
diff --git a/vecs.cc b/vecs.cc
index 1007d331505e3d61b9d0ed13378b340786915412..9ac40b44a45c50e14f26a3dc83c0ded6e23e5c5a 100644 (file)
--- a/vecs.cc
+++ b/vecs.cc
@@ -799,61 +799,65 @@ void Vecs::validate_options(const QStringList& options, const QVector<arglist_t>
   }
 }
 
-Format* Vecs::find_vec(const QString& vecname)
+void Vecs::prepare_format(const fmtinfo_t& fmtdata) const
 {
-  QStringList options = vecname.split(',');
-  if (options.isEmpty()) {
-    fatal("A format name is required.\n");
-  }
-  const QString svecname = options.takeFirst();
+  QVector<arglist_t>* args = fmtdata->get_args();
 
-  for (const auto& vec : d_ptr_->vec_list) {
-    if (svecname.compare(vec.name, Qt::CaseInsensitive) != 0) {
-      continue;
-    }
+  validate_options(fmtdata.options, args, fmtdata.fmtname);
 
-    QVector<arglist_t>* args = vec.vec->get_args();
-
-    validate_options(options, args, vec.name);
-
-    if (args && !args->isEmpty()) {
-      assert(args->isDetached());
-      for (auto& arg : *args) {
-        if (!options.isEmpty()) {
-          const QString opt = get_option(options, arg.argstring);
-          if (!opt.isNull()) {
-            assign_option(vec.name, &arg, opt);
-            continue;
-          }
-        }
-        QString qopt = inifile_readstr(global_opts.inifile, vec.name, arg.argstring);
-        if (qopt.isNull()) {
-          qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring);
-        }
-        if (qopt.isNull()) {
-          assign_option(vec.name, &arg, arg.defaultvalue);
-        } else {
-          assign_option(vec.name, &arg, qopt);
+  if (args && !args->isEmpty()) {
+    assert(args->isDetached());
+    for (auto& arg : *args) {
+      if (!fmtdata.options.isEmpty()) {
+        const QString opt = get_option(fmtdata.options, arg.argstring);
+        if (!opt.isNull()) {
+          assign_option(fmtdata.fmtname, &arg, opt);
+          continue;
         }
       }
+      QString qopt = inifile_readstr(global_opts.inifile, fmtdata.fmtname, arg.argstring);
+      if (qopt.isNull()) {
+        qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring);
+      }
+      if (qopt.isNull()) {
+        assign_option(fmtdata.fmtname, &arg, arg.defaultvalue);
+      } else {
+        assign_option(fmtdata.fmtname, &arg, qopt);
+      }
     }
+  }
 
-    if (global_opts.debug_level >= 1) {
-      disp_vec_options(vec.name, args);
-    }
+  if (global_opts.debug_level >= 1) {
+    disp_vec_options(fmtdata.fmtname, args);
+  }
 
 #if CSVFMTS_ENABLED
-    /*
-     * If this happens to be xcsv,style= and it was preceeded by an xcsv
-     * format that utilized an internal style file, then we need to let
-     * xcsv know the internal style file is no longer in play.
-     */
-    d_ptr_->xcsv_fmt.xcsv_setup_internal_style(nullptr);
+  /*
+   * For style based formats let xcsv know the style file.  Otherwise
+   * make sure xcsv knows no style file is in use. This covers the case
+   * that we are processing xcsv,style= and it was preceeded by an xcsv
+   * format that utilized an internal style file.
+   */
+  d_ptr_->xcsv_fmt.xcsv_setup_internal_style(fmtdata.style_filename);
 #endif // CSVFMTS_ENABLED
-    vec.vec->set_name(vec.name);       /* needed for session information */
-    vec.vec->set_argstring(vecname);  /* needed for positional parameters */
-    return vec.vec;
+}
 
+Vecs::fmtinfo_t Vecs::find_vec(const QString& fmtargstring)
+{
+  QStringList options = fmtargstring.split(',');
+  if (options.isEmpty()) {
+    fatal("A format name is required.\n");
+  }
+  const QString fmtname = options.takeFirst();
+
+  for (const auto& vec : d_ptr_->vec_list) {
+    if (fmtname.compare(vec.name, Qt::CaseInsensitive) != 0) {
+      continue;
+    }
+
+    fmtinfo_t fmtinfo{vec.vec, vec.name, nullptr, options};
+    prepare_format(fmtinfo);
+    return fmtinfo;
   }
 
   /*
@@ -861,52 +865,19 @@ Format* Vecs::find_vec(const QString& vecname)
    * is to search the list of xcsv styles.
    */
   for (const auto& svec : qAsConst(style_list)) {
-    if (svecname.compare(svec.name,  Qt::CaseInsensitive) != 0) {
+    if (fmtname.compare(svec.name,  Qt::CaseInsensitive) != 0) {
       continue;
     }
 
-    QVector<arglist_t>* xcsv_args = d_ptr_->vec_list.at(0).vec->get_args();
-
-    validate_options(options, xcsv_args, svec.name);
-
-    if (xcsv_args && !xcsv_args->isEmpty()) {
-      assert(xcsv_args->isDetached());
-      for (auto& arg : *xcsv_args) {
-        if (!options.isEmpty()) {
-          const QString opt = get_option(options, arg.argstring);
-          if (!opt.isNull()) {
-            assign_option(svec.name, &arg, opt);
-            continue;
-          }
-        }
-        QString qopt = inifile_readstr(global_opts.inifile, svec.name, arg.argstring);
-        if (qopt.isNull()) {
-          qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring);
-        }
-        if (qopt.isNull()) {
-          assign_option(svec.name, &arg, arg.defaultvalue);
-        } else {
-          assign_option(svec.name, &arg, qopt);
-        }
-      }
-    }
-
-    if (global_opts.debug_level >= 1) {
-      disp_vec_options(svec.name, xcsv_args);
-    }
-#if CSVFMTS_ENABLED
-    d_ptr_->xcsv_fmt.xcsv_setup_internal_style(svec.style_filename);
-#endif // CSVFMTS_ENABLED
-
-    d_ptr_->vec_list[0].vec->set_name(svec.name);      /* needed for session information */
-    d_ptr_->vec_list[0].vec->set_argstring(vecname);  /* needed for positional parameters */
-    return d_ptr_->vec_list[0].vec;
+    fmtinfo_t fmtinfo{d_ptr_->vec_list.at(0).vec, svec.name, svec.style_filename, options};
+    prepare_format(fmtinfo);
+    return fmtinfo;
   }
 
   /*
    * Not found.
    */
-  return nullptr;
+  return {};
 }
 
 /*
diff --git a/vecs.h b/vecs.h
index b0f9ebe90bc03617386d804a057a91aba25a0517..dc9193cc1fab79359e46ba92fa2be1a386a5aee7 100644 (file)
--- a/vecs.h
+++ b/vecs.h
@@ -35,6 +35,25 @@ class Vecs
 {
 // Meyers Singleton
 public:
+
+  /* Types */
+
+  class fmtinfo_t {
+  public:
+
+    explicit operator bool() const {
+      return fmt != nullptr;
+    }
+    Format* operator->() const {
+      return fmt;
+    }
+
+    Format* fmt{};
+    QString fmtname;
+    QString style_filename;
+    QStringList options;
+  };
+
   /* Special Member Functions */
 
   static Vecs& Instance();
@@ -51,7 +70,8 @@ public:
   static void disp_vec_options(const QString& vecname, const QVector<arglist_t>* args);
   static void validate_options(const QStringList& options, const QVector<arglist_t>* args, const QString& name);
   static QString get_option(const QStringList& options, const QString& argname);
-  Format* find_vec(const QString& vecname);
+  void prepare_format(const fmtinfo_t& data) const;
+  fmtinfo_t find_vec(const QString& fmtargstring);
   void disp_vecs() const;
   void disp_vec(const QString& vecname) const;
   static const char* name_option(uint32_t type);